package com.zhang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * 测试Servlet
 */
public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求编码集
        req.setCharacterEncoding("utf8");
        //设置响应信息的MIME类型的编码集
        resp.setContentType("text/html;charset=utf-8");

        //获取前端传过来的表单数据
        StringBuffer info = new StringBuffer();
        info.append("用户名: " + req.getParameter("username") + "<br>");
        info.append("性别: " + req.getParameter("sex") + "<br>");
        info.append("学历: " +req.getParameter("edu")+"<br>");
        //获取表单名对应的表单值
        String[] hobbies = req.getParameterValues("hobby");
        info.append("爱好: "+ Arrays.toString(hobbies));

        //得到写入流，向客户端输出文本数据
        PrintWriter out = resp.getWriter();
        out.println(info);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
